package com.googlecode.maurersmarbles.drawables;

import java.util.Map;
import java.util.TreeMap;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;

import com.googlecode.maurersmarbles.Card;
import com.googlecode.maurersmarbles.R;
import com.googlecode.maurersmarbles.Util;

public class ImageCardDrawable extends CardDrawable {

	/**
	 * The map that contains the bitmap for each card.
	 */
	private static final Map<Integer, Map<Character, Bitmap>> CARD_TO_BITMAP_MAP = new TreeMap<Integer, Map<Character, Bitmap>>();
	/**
	 * The paint used to draw the selection.
	 */
	private static Paint SELECTED_PAINT;

	/**
	 * Creates a new ImageCardDrawable.
	 * @param card the card to draw
	 * @param corners the corners of the card on the screen
	 */
	public ImageCardDrawable(Card card, Point[] corners) {
		this(card, corners, false);
	}

	/**
	 * Creates a new ImageCardDrawable.
	 * @param card the card to draw
	 * @param corners the corners of the card on the screen
	 * @param selected whether or not the card is selected
	 */
	public ImageCardDrawable(Card card, Point[] corners, boolean selected) {
		super(card, corners, selected);
	}

	/**
	 * Sets the context and loads the bitmap images.
	 * @param context the context
	 */
	public static void setContext(Context context) {

		Resources resources = context.getResources();

		CARD_TO_BITMAP_MAP.clear();

		CARD_TO_BITMAP_MAP.put(Card.CARD_ID_A, new TreeMap<Character, Bitmap>());
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_A).put(Card.CARD_SUIT_CLUBS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_ac));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_A).put(Card.CARD_SUIT_DIAMONDS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_ad));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_A).put(Card.CARD_SUIT_HEARTS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_ah));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_A).put(Card.CARD_SUIT_SPADES, BitmapFactory.decodeResource(resources, R.drawable.classic_card_as));

		CARD_TO_BITMAP_MAP.put(Card.CARD_ID_2, new TreeMap<Character, Bitmap>());
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_2).put(Card.CARD_SUIT_CLUBS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_2c));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_2).put(Card.CARD_SUIT_DIAMONDS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_2d));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_2).put(Card.CARD_SUIT_HEARTS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_2h));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_2).put(Card.CARD_SUIT_SPADES, BitmapFactory.decodeResource(resources, R.drawable.classic_card_2s));

		CARD_TO_BITMAP_MAP.put(Card.CARD_ID_3, new TreeMap<Character, Bitmap>());
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_3).put(Card.CARD_SUIT_CLUBS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_3c));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_3).put(Card.CARD_SUIT_DIAMONDS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_3d));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_3).put(Card.CARD_SUIT_HEARTS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_3h));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_3).put(Card.CARD_SUIT_SPADES, BitmapFactory.decodeResource(resources, R.drawable.classic_card_3s));

		CARD_TO_BITMAP_MAP.put(Card.CARD_ID_4, new TreeMap<Character, Bitmap>());
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_4).put(Card.CARD_SUIT_CLUBS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_4c));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_4).put(Card.CARD_SUIT_DIAMONDS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_4d));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_4).put(Card.CARD_SUIT_HEARTS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_4h));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_4).put(Card.CARD_SUIT_SPADES, BitmapFactory.decodeResource(resources, R.drawable.classic_card_4s));

		CARD_TO_BITMAP_MAP.put(Card.CARD_ID_5, new TreeMap<Character, Bitmap>());
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_5).put(Card.CARD_SUIT_CLUBS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_5c));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_5).put(Card.CARD_SUIT_DIAMONDS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_5d));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_5).put(Card.CARD_SUIT_HEARTS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_5h));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_5).put(Card.CARD_SUIT_SPADES, BitmapFactory.decodeResource(resources, R.drawable.classic_card_5s));

		CARD_TO_BITMAP_MAP.put(Card.CARD_ID_6, new TreeMap<Character, Bitmap>());
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_6).put(Card.CARD_SUIT_CLUBS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_6c));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_6).put(Card.CARD_SUIT_DIAMONDS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_6d));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_6).put(Card.CARD_SUIT_HEARTS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_6h));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_6).put(Card.CARD_SUIT_SPADES, BitmapFactory.decodeResource(resources, R.drawable.classic_card_6s));

		CARD_TO_BITMAP_MAP.put(Card.CARD_ID_7, new TreeMap<Character, Bitmap>());
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_7).put(Card.CARD_SUIT_CLUBS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_7c));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_7).put(Card.CARD_SUIT_DIAMONDS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_7d));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_7).put(Card.CARD_SUIT_HEARTS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_7h));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_7).put(Card.CARD_SUIT_SPADES, BitmapFactory.decodeResource(resources, R.drawable.classic_card_7s));

		CARD_TO_BITMAP_MAP.put(Card.CARD_ID_8, new TreeMap<Character, Bitmap>());
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_8).put(Card.CARD_SUIT_CLUBS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_8c));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_8).put(Card.CARD_SUIT_DIAMONDS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_8d));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_8).put(Card.CARD_SUIT_HEARTS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_8h));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_8).put(Card.CARD_SUIT_SPADES, BitmapFactory.decodeResource(resources, R.drawable.classic_card_8s));

		CARD_TO_BITMAP_MAP.put(Card.CARD_ID_9, new TreeMap<Character, Bitmap>());
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_9).put(Card.CARD_SUIT_CLUBS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_9c));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_9).put(Card.CARD_SUIT_DIAMONDS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_9d));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_9).put(Card.CARD_SUIT_HEARTS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_9h));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_9).put(Card.CARD_SUIT_SPADES, BitmapFactory.decodeResource(resources, R.drawable.classic_card_9s));

		CARD_TO_BITMAP_MAP.put(Card.CARD_ID_10, new TreeMap<Character, Bitmap>());
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_10).put(Card.CARD_SUIT_CLUBS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_10c));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_10).put(Card.CARD_SUIT_DIAMONDS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_10d));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_10).put(Card.CARD_SUIT_HEARTS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_10h));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_10).put(Card.CARD_SUIT_SPADES, BitmapFactory.decodeResource(resources, R.drawable.classic_card_10s));

		CARD_TO_BITMAP_MAP.put(Card.CARD_ID_J, new TreeMap<Character, Bitmap>());
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_J).put(Card.CARD_SUIT_CLUBS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_jc));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_J).put(Card.CARD_SUIT_DIAMONDS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_jd));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_J).put(Card.CARD_SUIT_HEARTS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_jh));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_J).put(Card.CARD_SUIT_SPADES, BitmapFactory.decodeResource(resources, R.drawable.classic_card_js));

		CARD_TO_BITMAP_MAP.put(Card.CARD_ID_Q, new TreeMap<Character, Bitmap>());
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_Q).put(Card.CARD_SUIT_CLUBS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_qc));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_Q).put(Card.CARD_SUIT_DIAMONDS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_qd));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_Q).put(Card.CARD_SUIT_HEARTS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_qh));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_Q).put(Card.CARD_SUIT_SPADES, BitmapFactory.decodeResource(resources, R.drawable.classic_card_qs));

		CARD_TO_BITMAP_MAP.put(Card.CARD_ID_K, new TreeMap<Character, Bitmap>());
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_K).put(Card.CARD_SUIT_CLUBS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_kc));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_K).put(Card.CARD_SUIT_DIAMONDS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_kd));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_K).put(Card.CARD_SUIT_HEARTS, BitmapFactory.decodeResource(resources, R.drawable.classic_card_kh));
		CARD_TO_BITMAP_MAP.get(Card.CARD_ID_K).put(Card.CARD_SUIT_SPADES, BitmapFactory.decodeResource(resources, R.drawable.classic_card_ks));

	}

	@Override
	public void drawCard(Canvas canvas, Card card, Point[] corners, boolean selected) {

		Bitmap bitmap = CARD_TO_BITMAP_MAP.get(card.getId()).get(card.getSuit());
		int x = corners[0].x;
		int y = corners[0].y;
		int height = corners[2].y - corners[0].y;
		int width = corners[2].x - corners[0].x;

		Util.drawImage(canvas, bitmap, x, y, width, height);

		if(selected) {
			Util.drawEmptyPolygon(canvas, corners, SELECTED_PAINT);
		}

	}

	static {		
		SELECTED_PAINT = new Paint();
		SELECTED_PAINT.setColor(Color.YELLOW);
		SELECTED_PAINT.setStrokeWidth(10);		
	}

}
